<script>
	import qqmap from '@/common/qqmap-wx-jssdk.js';
	import Vue from 'vue'
	export default {
		onLaunch: function() {
			Vue.prototype.AjaxUrl = "https://newcs.shengyisoft.com";
			
			// #ifdef MP-WEIXIN
			/*wx.cloud.init();
			var arry = [];
			const db = wx.cloud.database()
			const getCount = async function(){
				const countResult = await db.collection('wiki_character').count()
				const total = countResult.total;
				const batchTimes = Math.ceil(total / 20);
				const tasks = []
				for (let i = 0; i < batchTimes; i++) {
					const promise = db.collection('wiki_character').skip(i * 20).limit(20).get();
					tasks.push(promise)
				}
				return (await Promise.all(tasks)).reduce((acc, cur) => {
					let data = cur.data;
					acc.data = acc.data.concat(...data);
					return {
						data: acc.data,
						errMsg: acc.errMsg,
					}
				})
			}
			 db.collection('wiki_character').where({
			  _id: '5d2d8b256665917aae602a00'
			}).get({
			  success: res => {
				console.log('[数据库] [查询记录] 成功: ', res)
			  },
			  fail: err => {
				uni.showToast({
				  icon: 'none',
				  title: '查询记录失败'
				})
				console.error('[数据库] [查询记录] 失败：', err)
			  }
			}) 
			const wikidata = getCount();
			wikidata.then(function(result){
				console.info(result.data);
			});*/
			// db.collection('wiki_character').skip(20).limit(20).get({
			// 	success(res) {
			// 		console.log(res)
			// 	}
			// })
			/* db.collection('wiki_character').add({
				data: {
				  created: new Date().getTime(),
				  active: "排球",
				  month: 2,
				  place:"长安"
				},
				success: function(res) {
				  console.log(res)
				},fail: err => {
				  console.info("error",err);
				}
			}) */
			Vue.prototype.sig = 'T2ogIl5eEULcAtx3wY0G1pfSRAVFi1rt';
			let key2 = 'AJQBZ-G4YLX-HPX4W-TW6QY-O7P37-GTBBU';
			let _this = this;
			this.qqmapsdk = new qqmap({
				key: key2,
			});
			Vue.prototype.qqmapsdk = this.qqmapsdk;
			uni.showLoading({
				title: '获取信息中'
			});
			uni.getLocation({
				type: 'gcj02',
				success: function(res) {
					let latitude = res.latitude;
					let longitude = res.longitude;
					_this.qqmapsdk.reverseGeocoder({
						sig: _this.sig,
						location: {
							latitude: latitude,
							longitude: longitude
						},
						success: function(res) {
							let basic = res.result.ad_info.city;
							uni.getStorage({
								key: 'city_key',
								success(res1) {
									if(res1.data==undefined){
										uni.request({
											url: _this.AjaxUrl + '/WebApi/GetCity',
											success(res) {
												let detail = JSON.parse(res.data.Extend);
												let bools = false;
												detail.forEach(function(item){
													if(item.Name==basic.substr(0, basic.length - 1)){
														bools = true;
														uni.setStorage({
															key: 'city_key',
															data: {name:item.Name,key:item.Id},
															success: function() {
																console.log('1success');
															}
														});
													}
												});
												if(!bools){
												}
											},fail() {
												console.info("获取失败");
											}
										});
									}
								},fail(){
									uni.request({
										url: _this.AjaxUrl + '/WebApi/GetCity',
										success(res) {
											let detail = JSON.parse(res.data.Extend);
											let bools = false;
											detail.forEach(function(item){
												if(item.Name=="澳大利亚"){
													bools = true;
													uni.setStorage({
														key: 'city_key',
														data: {name:item.Name,key:item.Id},
														success: function() {
															console.log('1success');
														}
													});
												}
											});
											if(!bools){
												/* uni.showToast({
													title: '目前不支持当地城市，3s后跳往城市选择页',
													duration: 3000,
													icon:"none",
													mask:true
												});
												setTimeout(function() {
													uni.navigateTo({
														url: '/pages/citypage/indexes?type=ZH'
													});
												}, 3000); */
											}
										},fail() {
										}
									});
								}
							});
							uni.setStorage({
								key: 'local_city',
								data: basic.substr(0, basic.length - 1),
								success: function() {
									console.log('success');
								}
							});
							uni.hideLoading();
						},
						fail: function(err) {
							console.log(err);
						}
					})
				}
			});
			// #endif
			// #ifndef MP-WEIXIN
			/* uni.showLoading({  
				title: '获取信息中'  
			});  */

			new Promise(function(resolve, reject) {
				window.init = function() {
					resolve(qq)
				}
				var script = document.createElement("script");
				script.type = "text/javascript";
				script.src = "https://map.qq.com/api/js?v=2.exp&callback=init&key=AJQBZ-G4YLX-HPX4W-TW6QY-O7P37-GTBBU";
				script.onerror = reject;
				document.head.appendChild(script);
			}).then(qq => {
				var geocoder = new qq.maps.CityService({
					complete: function(result) {
						let basic = result.detail.detail.split(',')[1];
						uni.setStorage({
							key: 'city_key',
							data: basic.substr(0, basic.length - 1),
							success: function() {
								console.log('success');
							}
						});
						uni.setStorage({
							key: 'local_city',
							data: basic.substr(0, basic.length - 1),
							success: function() {
								console.log('success');
							}
						});
						uni.hideLoading();
					},
					error: function() {
						uni.hideLoading();
					}
				});
				uni.getLocation({
					type: 'gcj02',
					success: function(res) {
						let latitude = res.latitude;
						let longitude = res.longitude;
						var latLng = new qq.maps.LatLng(latitude, longitude);
						geocoder.searchCityByLatLng(latLng);
					}
				});
			})
			// #endif
			uni.getSystemInfo({
				success: function(e) {
					// #ifndef MP
					Vue.prototype.StatusBar = e.statusBarHeight;
					if (e.platform == 'android') {
						Vue.prototype.CustomBar = e.statusBarHeight + 50;
					} else {
						Vue.prototype.CustomBar = e.statusBarHeight + 45;
					};
					Vue.prototype.RightPaddingRight = 14;
					// #endif

					// #ifdef MP-WEIXIN
					Vue.prototype.StatusBar = e.statusBarHeight;
					let custom = wx.getMenuButtonBoundingClientRect();
					Vue.prototype.Custom = custom;
					Vue.prototype.CustomBar = custom.bottom + custom.top - e.statusBarHeight;
					Vue.prototype.RightPaddingRight = 0;
					// #endif		

					// #ifdef MP-ALIPAY
					Vue.prototype.StatusBar = e.statusBarHeight;
					Vue.prototype.CustomBar = e.statusBarHeight + e.titleBarHeight;
					// #endif
				}
			})
			Vue.prototype.ColorList = [{
					title: '嫣红',
					name: 'red',
					color: '#e54d42'
				},
				{
					title: '桔橙',
					name: 'orange',
					color: '#f37b1d'
				},
				{
					title: '明黄',
					name: 'yellow',
					color: '#fbbd08'
				},
				{
					title: '橄榄',
					name: 'olive',
					color: '#8dc63f'
				},
				{
					title: '森绿',
					name: 'green',
					color: '#39b54a'
				},
				{
					title: '天青',
					name: 'cyan',
					color: '#1cbbb4'
				},
				{
					title: '海蓝',
					name: 'blue',
					color: '#0081ff'
				},
				{
					title: '姹紫',
					name: 'purple',
					color: '#6739b6'
				},
				{
					title: '木槿',
					name: 'mauve',
					color: '#9c26b0'
				},
				{
					title: '桃粉',
					name: 'pink',
					color: '#e03997'
				},
				{
					title: '棕褐',
					name: 'brown',
					color: '#a5673f'
				},
				{
					title: '玄灰',
					name: 'grey',
					color: '#8799a3'
				},
				{
					title: '草灰',
					name: 'gray',
					color: '#aaaaaa'
				},
				{
					title: '墨黑',
					name: 'black',
					color: '#333333'
				},
				{
					title: '雅白',
					name: 'white',
					color: '#ffffff'
				},
			];
			uni.request({
				url: this.AjaxUrl + '/WebApi/GetTypeAllList',
				success(res) {
					let detail = JSON.parse(res.data.Extend);
					let detailJson = _this.atFirst(_this.groupBy(detail,item => item.GroupValue));
					for(let [index,item] of detailJson['"住宅出租方式"'].entries()){
						if(index==0){
							item.checked=true;
						}else{
							item.checked=false;
						}
					}
					uni.setStorage({
						key: 'TypeAllList',
						data:detailJson
					})
					Vue.prototype.TypeAllList = detailJson;
				},fail() {
				}
			});
			
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			groupBy(array, fn) {
			  let groups = {};
			  let i = 0
			  array.forEach(o => {
				o.checked = false;
				let group = JSON.stringify(fn(o));
				
				groups[group] = groups[group] || []
				groups[group].push(o)
			  })
			  return groups;
			  //return Object.keys(groups).map(group => groups[group])
			},
			atFirst(array){
				let item = {Key:null,DisplayValue:"不限"};
				item.checked = false;
				let obj1 = Object.keys(array);
				for(let i=0;i<obj1.length;i++){
					if(obj1[i]!='"住宅出租方式"'){
						array[obj1[i]].unshift(item);
					}
				}
				return array;
			}
		}
	}
</script>

<style lang="less">
	@import 'common/uni.css';
	@import "colorui/main.css";
	@import "colorui/icon.css";
	
	@import "static/main.less";

	@font-face {
		font-family: 'iconfont';
		src: url('https://at.alicdn.com/t/font_60881_hj0t7l80eyi.ttf') format('truetype');
	}

	.iconfont {
		font-family: 'iconfont';
	}

	.clearfix {
		overflow: hidden;
		_zoom: 1;
	}

	.clearfix:after {
		visibility: hidden;
		display: block;
		font-size: 0;
		content: " ";
		clear: both;
		height: 0;
	}

	/*每个页面公共css */
	body {
		//background: #ffffff;
		--red: #e54d42;
		--black2: #0a102b;
		background: url(http://p7.qhimg.com/t01a90765a503d2aa8d.jpg) fixed 50% 0 #d1d1d1;
		.text-red,
		.line-red,
		.lines-red {
			color: var(--red);
		}

		.text-darkGray,
		.line-darkGray,
		.lines-darkGray {
			color: var(--darkGray);
		}
		.text-black2,
		.line-black2,
		.lines-black2 {
			color: var(--black2);
		}
		.bg-black2 {
			background-color: var(--black2);
			color: var(--white);
		}
	}

	page {
		height: 100%;
	}

	video {
		z-index: 1;
	}

	.warp30 {
		width: 100%;
		display: block;
		height: 30upx;
		background: #f4f4f5;
	}

	.warp120 {
		width: 100%;
		display: block;
		height: 120upx;
		background: #f4f4f5;
	}

	/*  #ifdef MP-WEIXIN */
	.cu-tabbar-height.wx {
		height: 0;
		min-height: 0;
	}

	/*  #endif  */
	swiper.square-dot .wx-swiper-dot,
	swiper.square-dot .a-swiper-dot,
	swiper.square-dot .uni-swiper-dot {
		background: #ff5c5c;
	}

	.cu-bar.tabbar.shadow {
		-webkit-box-shadow: 0 1px 10px rgba(0, 0, 0, 0.2);
		box-shadow: 0 1px 10px rgba(0, 0, 0, 0.2);
	}

	[class*="cuIcon-"] {
		line-height: 1;
	}
	.cu-steps .cu-item{
		font-size: 12px;
	}
	.cu-steps .cu-item [class*="cuIcon-"], .cu-steps .cu-item .num{
		font-size: 15px;
		line-height: 20px;
	}
	.cu-steps .cu-item::before, .cu-steps .cu-item::after, .cu-steps.steps-arrow .cu-item::before, .cu-steps.steps-arrow .cu-item::after{
		width: calc(100% - 20px);
		left: calc(0px - (100% - 20px) / 2);
		top: 10px;
	}
	@keyframes show {
		0% {
			transform: translateY(-50px);
		}

		60% {
			transform: translateY(40upx);
		}

		100% {
			transform: translateY(0px);
		}
	}

	@-webkit-keyframes show {
		0% {
			transform: translateY(-50px);
		}

		60% {
			transform: translateY(40upx);
		}

		100% {
			transform: translateY(0px);
		}
	}
</style>
